package com.canaan.business.controller.pc.bill;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.business.domain.bill.TrayDetail;
import com.canaan.business.service.bill.ITrayDetailService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;

/**
 * 托盘明细Controller
 *
 * @author canaan
 * @date 2025-03-28
 */
@Api(tags = "托盘明细")
@RestController
@RequestMapping("/bill/trayDetail")
public class TrayDetailController extends BaseController {
    @Resource
    private ITrayDetailService trayDetailService;

    /**
     * 查询托盘明细列表
     */
    @ApiOperation(value = "查询托盘明细列表")
//    @PreAuthorize("@ss.hasPermi('bill:trayDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrayDetail trayDetail) {
        startPage();
        List<TrayDetail> list = trayDetailService.selectTrayDetailList(trayDetail);
        if (list==null || list.isEmpty()) {
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }

    /**
     * 导出托盘明细列表
     */
    @ApiOperation(value = "导出托盘明细列表")
    @PreAuthorize("@ss.hasPermi('bill:trayDetail:export')")
    @Log(title = "托盘明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrayDetail trayDetail) {
        List<TrayDetail> list = trayDetailService.selectTrayDetailList(trayDetail);
        ExcelUtil<TrayDetail> util = new ExcelUtil<TrayDetail>(TrayDetail.class);
        util.exportExcel(response, list, "托盘明细数据");
    }

    /**
     * 获取托盘明细详细信息
     */
    @ApiOperation(value = "获取托盘明细详细信息")
//    @PreAuthorize("@ss.hasPermi('bill:trayDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(trayDetailService.selectTrayDetailById(id));
    }

    /**
     * 新增托盘明细
     */
    @ApiOperation(value = "新增托盘明细")
//    @PreAuthorize("@ss.hasPermi('bill:trayDetail:add')")
    @Log(title = "托盘明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrayDetail trayDetail) {
        if (StrUtil.isEmpty(trayDetail.getTrayNo()) || !StrUtil.startWith(trayDetail.getTrayNo(), "T")) {
            AjaxResult.error("托盘号不正确");
        }
        trayDetail.setStockStatus("1");
        return toAjax(trayDetailService.insertTrayDetail(trayDetail));
    }

    /**
     * 修改托盘明细
     */
    @ApiOperation(value = "修改托盘明细")
    @PreAuthorize("@ss.hasPermi('bill:trayDetail:edit')")
    @Log(title = "托盘明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrayDetail trayDetail) {
        return toAjax(trayDetailService.updateTrayDetail(trayDetail));
    }

    /**
     * 删除托盘明细
     */
    @ApiOperation(value = "删除托盘明细")
    @PreAuthorize("@ss.hasPermi('bill:trayDetail:remove')")
    @Log(title = "托盘明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(trayDetailService.deleteTrayDetailByIds(ids));
    }

    /**
     * 获取托盘明细详细信息
     */
    @ApiOperation(value = "获取托盘明细详细信息")
//    @PreAuthorize("@ss.hasPermi('bill:trayDetail:query')")
    @GetMapping(value = "/details")
    public AjaxResult details(@RequestParam("trayNo") String trayNo) {
        TrayDetail trayDetail = new TrayDetail();
        trayDetail.setTrayNo(trayNo);
        if (StrUtil.isEmpty(trayNo) || !StrUtil.startWith(trayNo, "T")) {
            AjaxResult.error("托盘号不正确");
        }
         return success(trayDetailService.selectTrayDetailList(trayDetail));
    }

    /**
     * 清空托盘
     */
    @ApiOperation(value = "删除托盘明细")
//    @PreAuthorize("@ss.hasPermi('bill:trayDetail:remove')")
    @Log(title = "托盘明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteTrayDetailByTrayNo")
    public AjaxResult deleteTrayDetailByTrayNo(@RequestParam("trayNo") String trayNo) {
        return toAjax(trayDetailService.deleteTrayDetailByTrayNo(trayNo));
    }

    /**
     * 解绑物料
     */
    @ApiOperation("托盘明细查询")
    @PostMapping("/selectTrayDetails")
    public AjaxResult selectTrayDetails(@RequestBody TrayDetail vo)
    {
        return trayDetailService.selectTrayDetails(vo);
    }

    /**
     * 解绑物料
     */
    @ApiOperation("解绑物料")
    @Log(title = "解绑物料", businessType = BusinessType.UPDATE)
    @PostMapping("/unbindProductNo")
    public AjaxResult unbindProductNo(@RequestBody TrayDetail vo)
    {
        return trayDetailService.unbindProductNo(vo);
    }
}
